<?php
/* 直接购买
* @Author: Jerry-chen
* @Date:   2014-07-22 22:28:03
* @Last Modified time: 2014-07-24 19:17:40
* @Last Modified by: Jerry-chen<uranusbill@163.com>
* @ Copyright (c) 2014 http://www.cooldreamer.com All rightsreserved.
*/
class StraightbuyControl extends CommonControl
{
	
	function index()
	{
		//直接购买控制器里面，SESSION里面的控制值为1
		if(IS_POST && $_SESSION['step']==1)
		{
			// p($_POST);
			$data = Q('post.');
			//如果传过来是数组，则从选座里面过来
			if(is_array($data['goodsid']))
			{
				//选座里面的GOODSID是一样的商品
				$goodsid = $data['goodsid'][0];
				$goodsData = K('Goods')->get_one(array('goodsid'=>$goodsid),array('goodsid','goodsname'));
				//选座里面的时间ID是一样的商品
				$goodstimeid = $data['goodstimeid'][0];
				$time = K('Goodstime')->get_one(array('timeid'=>$goodstimeid),'time');
				// p($time);
				// p($goodsData);
				// 查出价格
				
				//根据GOODSPRICE查出
				//订单号生成格式
				$ordernumber='datami'.time();
				// 生成订单
					$orderData = array(
						'ordernumber'=>$ordernumber,
						'uid'=>session('uid'),
						'stadus'=>0,
						'ordertime'=>time(),
						);
					$orderid = K('order')->add_($orderData);
					// 因为价格不一样，所以循环价格来插入数据
					$count = 0;
					foreach ($data['goodsprice'] as $key => $value)
					 {
						$price = K('Goodsprice')->get_one(array('goodsprice'=>$value),'price');
						// p($price);
						$orderlistData = array(
						'orderid'=>$orderid,
						'goodsid'=>$goodsid,
						'timeid'=>$goodstimeid,
						'number'=>1,
						'recount'=>$price['price']*1,
						'price'=>$price['price'],
						'seatname'=>$data['seatsort'][$key]
						);
						// p($orderlistData);
						K('Orderlist')->add_($orderlistData);
						//订单步骤
						session('step',3);
						// 方便后面取订单号
						session('orderid',$orderid);
						
				}
			}else{
				// P(Q('post.'));
				// /选座里面的GOODSID是一样的商品
				$goodsid = $data['goodsid'];
				$goodsData = K('Goods')->get_one(array('goodsid'=>$goodsid),array('goodsid','goodsname'));
				//选座里面的时间ID是一样的商品
				$goodstimeid = $data['timeid'];
				$time = K('Goodstime')->get_one(array('timeid'=>$goodstimeid),'time');
				// p($time);
				// p($goodsData);
				// 查出价格
				
				//根据GOODSPRICE查出
				//订单号生成格式
				$ordernumber='datami'.time();
				// 生成订单
				$price = K('Goodsprice')->get_one(array('goodsprice'=>$data['goodsprice']),'price');
					$orderData = array(
						'ordernumber'=>$ordernumber,
						'uid'=>session('uid'),
						'stadus'=>0,
						'ordertime'=>time(),
						);
					// p($orderData);

					$orderid = K('order')->add_($orderData);
					$orderlistData = array(
						'orderid'=>$orderid,
						'goodsid'=>$goodsid,
						'timeid'=>$goodstimeid,
						'number'=>$data['number'],
						'recount'=>$price['price']*$data['number'],
						'price'=>$price['price'],
						);
						// p($orderlistData);
						K('Orderlist')->add_($orderlistData);
						// p($orderlistData);
					//订单步骤
					session('step',3);
					// 方便后面取订单号
					session('orderid',$orderid);

			}
		}
			
			//订单号生成格式
			//根据订单号查出同一订单的商品，然后分配数据
			$orderdata = K('Order')->get_all(array('uid'=>session('uid'),'orderid'=>session('orderid')));
			// p($orderdata);
			$count = 0;
			foreach ($orderdata as $k => $v) 
			{
				$orderlist = K('Orderlist')->get_all(array('orderid'=>$v['orderid']),array('timeid','goodsid','price','number','recount','seatname'));
				// p($orderlist);
				$orderdata[$k]['orderlist'] = $orderlist;
					foreach ($orderlist as $key => $value)
				 {
						// 根据时间ID，查出时间
					$timename = K('Goodstime')->get_one(array('timeid'=>$value['timeid']),array('time'));
					$orderdata[$k]['orderlist'][$key]['time'] = $timename['time'];
					//根据货品ID查出货物名
					$goodsname = K('Goods')->get_one(array('goodsid'=>$value['goodsid']),array('goodsname'));
					$orderdata[$k]['orderlist'][$key]['goodsname'] = $goodsname['goodsname'];
					//价格
					$orderdata[$k]['orderlist'][$key]['price'] = $value['price'];
					$orderdata[$k]['orderlist'][$key]['number'] = $value['number'];
					$orderdata[$k]['orderlist'][$key]['recount'] = $value['recount'];
					$count += $value['recount'];
					//订单合计
				}
			}	
			
			//分配订单数据
			$this->assign('orderdata',$orderdata);
			// p($orderdata);
			$this->assign('count',$count);

		//收货地址管理
		$spData = K('Spaddress')->get_one(array('uid'=>session('uid')));
		$this->assign('spData',$spData);
		// p(Q('post.'));	
		$this->display('Straightbuy');

	}



}